package com.wk.pms.controller;

import com.wk.common.bean.PageVo;
import com.wk.common.bean.QueryCondition;
import com.wk.common.bean.Resp;
import com.wk.pms.entity.AttrGroup;
import com.wk.pms.service.AttrGroupService;
import com.wk.pms.vo.AttrGroupVO;
import com.wk.pms.vo.ItemGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/attrgroup")
public class AttrGroupController {

    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 根据spuId和分类ID查询spu对应的所有分类信息及值
     */
    @GetMapping("item/group/{cid}/{spuId}")
    public Resp<List<ItemGroupVO>> queryItemGroupByCategoryIdAndSpuId(@PathVariable(value = "cid") Long categoryId,
                                                                      @PathVariable(value = "spuId") Long spuId){
        List<ItemGroupVO> itemGroupVos = attrGroupService.queryItemGroupByCategoryIdAndSpuId(categoryId,spuId);
        return Resp.ok(itemGroupVos);
    }

    /**
     * 页面调用位置：添加spu -> 录入spu属性信息
     * 根据categoryId查询三级分类下的所有属性分组，根据分组中的ID查询中间表,再根据中间表的attrids查询参数信息
     */
    @GetMapping("withattrs/cat/{categoryId}")
    public Resp<List<AttrGroupVO>> queryGroupWithAttrsByCategoryId(@PathVariable Long categoryId){
        List<AttrGroupVO> attrGroupVo = attrGroupService.queryGroupWithAttrsByCategoryId(categoryId);
        return Resp.ok(attrGroupVo);
    }

    /**
     * 根据分组ID查询分组数据和属性
     */
    @GetMapping("withattr/{groupId}")
    public Resp<AttrGroupVO> queryGroupWithAttrsByGroupId(@PathVariable("groupId") Long groupId){
        AttrGroupVO groupVo = attrGroupService.queryGroupWithAttrsByGroupId(groupId);
        return Resp.ok(groupVo);
    }

    /**
     * 根据分类ID查询分组数据并分页
     */
    @GetMapping("{catId}")
    public Resp<PageVo> queryGroupByPage(QueryCondition condition, @PathVariable("catId") Long catId) {
        PageVo pageVo = attrGroupService.queryGroupByPage(condition,catId);
        return Resp.ok(pageVo);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Resp<Object> save(@RequestBody AttrGroup attrGroup){
        attrGroupService.save(attrGroup);

        return Resp.ok(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Resp<Object> update(@RequestBody AttrGroup attrGroup){
        attrGroupService.updateById(attrGroup);

        return Resp.ok(null);
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Resp<Object> delete(@RequestBody Long[] attrGroupIds){
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return Resp.ok(null);
    }
}

